/* 
 * Autores:
 *      Jonathan Lopez  11106
 *      Jorge Martínez  11237
 * 
 * Ultima revision: $Rev: 28 $
 *      Fecha: $Date: 2011-11-13 08:23:56 +0000 (Sun, 13 Nov 2011) $
 * 
 * Archivo: InicioSesion.java
 */

package GUI;

import Servicios.Usuario;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JOptionPane;

/**
 * Contiene los elementos para que un usuario pueda iniciar su sesion en el
 * programa.
 * 
 * @author Jorge
 * @author Jonathan
 */
public class InicioSesion extends javax.swing.JPanel {
    
    /** Creates new form InicioSesion */
    public InicioSesion( FramePrincipal frmPrincipal ) {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtUsuario = new javax.swing.JTextField();
        txtPassword = new javax.swing.JPasswordField();
        btnIniciar = new javax.swing.JButton();
        lblAgregarUsuario = new javax.swing.JLabel();

        txtUsuario.setColumns(20);
        txtUsuario.setForeground(java.awt.Color.lightGray);
        txtUsuario.setText("<usuario>");
        txtUsuario.setMaximumSize(new java.awt.Dimension(20, 1));
        txtUsuario.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtUsuarioMouseClicked(evt);
            }
        });
        txtUsuario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtUsuarioKeyTyped(evt);
            }
        });

        txtPassword.setColumns(20);

        btnIniciar.setText("Iniciar Sesión");
        btnIniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIniciarActionPerformed(evt);
            }
        });

        lblAgregarUsuario.setForeground(java.awt.SystemColor.activeCaption);
        lblAgregarUsuario.setText("Agregar Usuario");
        lblAgregarUsuario.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lblAgregarUsuario.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblAgregarUsuarioMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblAgregarUsuarioMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblAgregarUsuarioMouseExited(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtPassword)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lblAgregarUsuario)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
                        .addComponent(btnIniciar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnIniciar)
                    .addComponent(lblAgregarUsuario))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void lblAgregarUsuarioMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblAgregarUsuarioMouseEntered
        Font f = lblAgregarUsuario.getFont();
        f = f.deriveFont(Font.BOLD);
        lblAgregarUsuario.setFont(f);
    }//GEN-LAST:event_lblAgregarUsuarioMouseEntered

    private void lblAgregarUsuarioMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblAgregarUsuarioMouseExited
        Font f = lblAgregarUsuario.getFont();
        f = f.deriveFont(Font.PLAIN);
        lblAgregarUsuario.setFont(f);
    }//GEN-LAST:event_lblAgregarUsuarioMouseExited

    private void txtUsuarioKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtUsuarioKeyTyped
        if( txtUsuario.getText().equals( "<usuario>" ) ){
            txtUsuario.setText( "" );
            txtUsuario.setForeground(Color.BLACK);
        }
    }//GEN-LAST:event_txtUsuarioKeyTyped

    private void btnIniciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIniciarActionPerformed
        // Validar al usuario
        String pass = String.copyValueOf( txtPassword.getPassword() );
        Servicios.Usuario usuarioIngresado = new Usuario(txtUsuario.getText(),
                pass );
        
        if( FramePrincipal.obtenerServicios().cambiarUsuario(usuarioIngresado) ){
            // Si el usuario esta validado, iniciar el otro frame
            new PanelCategoria( usuarioIngresado );
            txtUsuario.setText("");
            txtPassword.setText("");
            return;
        } else {
            // Si el usuario no es valido, mostrar un dialogo con el error
            JOptionPane.showMessageDialog( this, 
                    "El usuario y contraseña no son válidos." );
        }
    }//GEN-LAST:event_btnIniciarActionPerformed

    private void txtUsuarioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtUsuarioMouseClicked
        if( txtUsuario.getText().equals( "<usuario>" ) ){
            txtUsuario.setText( "" );
            txtUsuario.setForeground(Color.BLACK);
        }
    }//GEN-LAST:event_txtUsuarioMouseClicked

    private void lblAgregarUsuarioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblAgregarUsuarioMouseClicked
        // Cambiar el panel creando uno nuevo
        new NuevoUsuario();
        txtUsuario.setText("");
        txtPassword.setText("");
    }//GEN-LAST:event_lblAgregarUsuarioMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnIniciar;
    private javax.swing.JLabel lblAgregarUsuario;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
}
